#!/usr/bin/env node

const ms = [];  //ms = middlewaress
const log = console.log;
/*---------------------*/
function m1(next) {
  log(1);next();log(2);
}

function m2(next) {
    log(3);next();log(4);
}

function m3(next) {
    log(5);next();log(6);
}

function m4(next) {
    log(7);next();log(8);
}

use(m1);
use(m2);
use(m3);
use(m4);

/*--------------------*/

function use(mx) {
    ms.push(mx);
}
const compose = (i=0) =>
    (i == ms.length)
        ? () => log('end')
        : ()=> ms[i](compose(i+1));


compose()();


